﻿using System.IO;
using MapDownloader.Core.Models;

namespace MapDownloader.Core.Core
{
   /// <summary>
   ///    图片代理类
   /// </summary>
   public abstract class BaseImageProxy
   {
      public abstract BaseImage FromStream(Stream stream);
      /// <summary>
      /// 保存
      /// </summary>
      /// <param name="stream">流</param>
      /// <param name="image">文件</param>
      /// <returns></returns>
      public abstract bool Save(Stream stream, BaseImage image);

      public BaseImage FromArray(byte[] data)
      {
         var memoryStream = new MemoryStream(data, 0, data.Length, false, true);
         var pi = FromStream(memoryStream);
         if (pi != null)
         {
            memoryStream.Position = 0;
            pi.Data = memoryStream;
         }
         else
         {
            memoryStream.Dispose();
         }

         return pi;
      }
   }
}